<?php
/**
 * Created by PhpStorm.
 * User: 式神 (luck48.com)
 * Email: 289650682@qq.com
 * Name: ${NAME}Administrator
 * Date: 2017-03-30
 * Time: 15:20
 */
namespace App\Http\Controllers\Admin;

use App\Http\Models\Attr;
use App\Http\Models\Category;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class AttrController extends CommonController{
    //推荐位列表
    public function attrlist(Request $request){
        if($request->isMethod('post')){
            $pagenum=config('system.page_num');
            $p=Input::get('p')?Input::get('p'):1;
            $json=[];
            $model=new Attr();
            $count=$model->count();
            $list=$model->skip(($p-1)*$pagenum)->take($pagenum)->orderBy('id','asc')->get()->toArray();
            $lists=[];
            foreach($list as $v){
                if($v['cid']!=0){
                    $v['catname']=Category::where('id',$v['cid'])->first()->name;
                }else{
                    $v['catname']='All';
                }
                array_push($lists,$v);
            }
            $data['list']=$lists;
            $data['title']=trans('config.listtitle');
            $json['status']=200;
            $json['pages']=ceil($count/$pagenum);
            $json['data']=$data;
            return $json;
        }
        return view('admin.attr.list');
    }
    /**
     * 添加推荐位
     * @param Request $request
     * @return post
     */
    public function attrAdd(Request $request){
        if($request->isMethod('post')){
            $input=Input::except('_token');
            DB::beginTransaction();
            $data=[];
            $input['listsort']=$input['listsort']?$input['listsort']:0;
            try{
                Attr::create($input);
                DB::commit();
                $data['status']=200;
                $data['msg']=trans('config.submit_success');
                $data['url']='reload';
            }catch (\Exception $e) {
                DB::rollBack();
                $data['status']=300;
                $data['msg']=trans('config.submit_error');
            }
            return $data;
        }
        $cate=Category::all();
        $cates=catlist($cate);
        return view('admin.attr.add',compact('cates'));
    }

    /**
     * 编辑推荐位
     * @param Request $request
     * @return post
     */
    public function attrEdit(Request $request){
        if($request->isMethod('post')){
            $input=Input::except('_token','id','file');
            if(Attr::where('id',Input::get('id'))->update($input)){
                $data['status']=200;
                $data['msg']=trans('config.submit_success');
                if(isset($input['name'])){
                    $data['url']='reload';
                }
            }else{
                $data['status']=300;
                $data['msg']=trans('config.submit_error');
            }
            return $data;
        }

        $id=Input::get('id')?Input::get('id'):0;
        if($id==0){
            return trans('config.return_error');
        }
        $data=Attr::where('id',$id)->first();
        $pid=$data->cid;
        $cate=Category::all();
        $cates=catlist($cate);
        return view('admin.attr.edit',compact('cates','pid','data'));
    }
    //删除推荐位
    public function attrDel(Request $request){
        if($request->isMethod('post')){
            $id=Input::get('id');
            $id=explode(",", $id);
            $m=new Attr();
            DB::beginTransaction();
            try{
                $m->whereIn('id',$id)->delete();
                DB::commit();
                $data['status']=200;
                $data['msg']=trans('config.submit_success');
                $data['url']='reload';
            }catch (\Exception $e) {
                DB::rollBack();
                $data['status']=300;
                $data['msg']=trans('config.submit_error');
            }
            return $data;
        }
    }
}